<?php
/**
 * Created by PhpStorm.
 * User: Rei
 * Date: 16/5/6
 * Time: 15:01
 * Mail: rei@king.moe
 * Des: 检测登陆
 */

namespace Home\Controller;

use Think\Controller;

class LoginController extends Controller
{
    //验证登陆
    public function _initialize()
    {
        if (!session('?uid')) {
            $islogin = cookie('islogin');
            if(!$islogin){
                redirect('/Home/User/login');
            }else{
                $islogin = D('User')->verifyUserToken($islogin);
                if($islogin){
                    //token验证成功
                    session('uid', $islogin);
                    D('User')->updateLogTime($islogin);
                }else{
                    //token验证失败
                    cookie('islogin', null);
                    redirect('/Home/User/login');
                }
            }
        }else{
            if(!cookie('islogin')){
                $token = D('User')->getUserToken(session('uid'));
                cookie('islogin',$token,3600*24*7); // cookie保存1周
            }
        }
    }
}